﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO;
using BO.Custom;
using Business;
using Entity;
using FreshCms.BackEnd.ControlsCommon;
using Utilities;

namespace FreshCms.BackEnd.Controls.Product
{
    public partial class Detail : System.Web.UI.UserControl
    {
        public Int32 id = Utils.GetQuery("id", 0);
        public string pageTitle = string.Empty;
        public string btnSubmitText = string.Empty;
        public string avatar = string.Empty;
        #region For ListOn
        public string strChildListOn = string.Empty;
        public string strParentListOn = string.Empty;
        #endregion
        public OutProduct obj = new OutProduct();

        #region check login and permission
        InPms chkPms = new InPms();
        public Int32 panelId = 0;
        private void checkLogin()
        {
            // Check and load UC
            if (Request.QueryString["ui"] != null)
                Int32.TryParse(Request.QueryString["ui"], out panelId);
            // check Pms
            if (panelId > 0)
                chkPms = InPmsBusiness.CheckSessionPms(panelId);
            if (chkPms == null)
                Response.Redirect("/BackEnd/xlogin.aspx?redirect=" + Utils.GetCurrentCmsPage().Replace("?", "@").Replace("&", "|"));
            // show Pms in Top Bar
            TopBarPms ucTopBar = Page.Master.FindControl("TopBarPms") as TopBarPms;
            ucTopBar.BindData(chkPms);
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            checkLogin();
            if (!Page.IsPostBack)
            {
                if (chkPms.PmsView == 1)
                {
                    BindStatus();
                    BindListCate();
                    BindData();
                }
                CkInit();
            }
        }

        private void CkInit()
        {
            CKDetail.Language = "vi";
            CKDetail.BasePath = "/BackEnd/js/ckeditor/";
            CKDetail.Skin = "office2003";
            //CKEditorControl1.config.filebrowserImageBrowseUrl = "/ckfinder/ckfinder.html?type=Images";
            //CKEditorControl1.config.filebrowserImageUploadUrl = "/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images";
            CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
            _FileBrowser.BasePath = "/BackEnd/js/ckfinder/";
            _FileBrowser.SetupCKEditor(CKDetail);
        }

        private void BindData()
        {
            avatar = Constant.ProductAvatarDefault;
            if (id == 0)
            {
                pageTitle = "Thêm mới sản phẩm";
                btnSubmitText = "Thêm mới";
                //phAddnew.Visible = true;
                return;
            }
            obj = OutProductBusiness.GetOutProductById(id);
            if (obj != null)
            {
                pageTitle = "Chi tiết sản phẩm: \"" + obj.ProName + "\"";
                btnSubmitText = "Cập nhật";
                ddlStt.SelectedValue = obj.ProStatus.ToString();
                CKDetail.Text = obj.ProDetail1;
                avatar = obj.ThumbImage1;
                // Lây danh sách list on sản phẩm
                var lstListon = OutProductListOnBusiness.GetOutProductListOnByProId(id);
                if(lstListon!=null && lstListon.Count>0)
                {
                    foreach (var item in lstListon)
                    {
                        if(item.IsParent==1)
                            strParentListOn = strParentListOn + item.CatId.ToString() + ",";
                        else
                            strChildListOn = strChildListOn + item.CatId.ToString() + ",";
                    }
                    //strParentListOn = Utils.Left(strParentListOn, strParentListOn.Length - 1);
                    //strChildListOn = Utils.Left(strChildListOn, strChildListOn.Length - 1);
                }
            }

        }

        private void BindStatus()
        {
            ddlStt.DataSource = StatusBusiness.GetShortListStatusForProduct(false, true);
            ddlStt.DataTextField = "Value";
            ddlStt.DataValueField = "Id";
            ddlStt.DataBind();
            // Bind color
            ddlStt.Items[0].Attributes.Add("style", Constant.StyleDropDisable);
            ddlStt.Items[1].Attributes.Add("style", Constant.StyleDropEnable);
            ddlStt.Items[2].Attributes.Add("style", Constant.StyleDropOrderProduct);
            ddlStt.Items[3].Attributes.Add("style", Constant.StyleDropOutOfProduct);
            ddlStt.Items[4].Attributes.Add("style", Constant.StyleDropDeleteProduct);

        }

        // Bind du liệu vào list Parent Cate
        private void BindListCate()
        {
            var lst = OutCategoryBusiness.GetOutCategoryList(0, 1);
            if (lst.Count > 0)
            {
                rptParentCate.DataSource = lst;
                rptParentCate.ItemDataBound += new RepeaterItemEventHandler(rptParentCate_ItemDataBound);
                rptParentCate.DataBind();
            }
        }

        void rptParentCate_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                try
                {
                    Repeater rptChildCate = (Repeater)e.Item.FindControl("rptChildCate");
                    OutCategory obj = (OutCategory)e.Item.DataItem;

                    // Bind Data to Child Category
                    var lst = OutCategoryBusiness.GetOutCategoryChildList(obj.CatId, 1);
                    if (lst.Count > 0)
                    {
                        rptChildCate.DataSource = lst;
                        //rptChildCate.ItemDataBound += new RepeaterItemEventHandler(rptChild_ItemDataBound);
                        rptChildCate.DataBind();
                    }
                }
                catch (Exception ex) { }

            }
        }

        //void rptChild_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        //{
        //    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        //    {
        //        try
        //        {
                    
        //        }
        //        catch (Exception ex) { }

        //    }
        //}

    }
}